package com.jysz.certificate.web.action.cm;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.CopyOfCertificationImporting;
import com.jysz.certificate.persistence.model.cm.CertificateRenew;
import com.jysz.certificate.persistence.model.cm.RenewObject;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.service.cm.CertificateRenewService;
import com.jysz.certificate.service.cm.RenewObjectService;
import com.jysz.certificate.service.system.BrandService;
import com.jysz.certificate.service.system.FinancialService;
import com.jysz.certificate.service.system.OrganizationService;
import com.jysz.certificate.service.system.ShopService;
import com.jysz.webcore.utils.SshBeanUtils;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/cm/renew/incf")
public class RenewObjectAction extends AbstractBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final static Logger LOG = LoggerFactory
			.getLogger(RenewObjectAction.class);

	private RenewObject search;
	private RenewObject entity;
	private CertificateRenew parent;

	private List<Brand> brands;// 主机厂
	private List<Financial> financials;// 金融机构

	@Resource
	private RenewObjectService renewObjectService;
	@Resource 
	private CertificateRenewService certificateRenewService;
	@Resource
	private FinancialService financialService;
	@Resource
	private BrandService brandService;
	@Resource
	private ShopService shopService;
	@Resource
	private OrganizationService organizationService;

	@Action(ADD)
	public String add() {
		financials = financialService.findAll();
		parent = certificateRenewService.find(parent.getId());
	 	 if(parent.getShop().getBrands() != null){
	 		 brands = new ArrayList<Brand>(parent.getShop().getBrands());
	 	 }
		setRedirectUrl(this.getContextPath() + "/cm/renew/edit.do?currentPage="
				+ getCurrentPage() + "&entity.id=" + parent.getId(), "search",
				search);
		return ADD;
	}
	@Action(EDIT)
	public String edit() {
		 financials = financialService.finList();
		 entity = renewObjectService.find(entity.getId());
		 parent = entity.getCertificateRenew();
		 
	 	 if(parent.getShop().getBrands() != null){
	 		 brands = new ArrayList<Brand>(parent.getShop().getBrands());
	 	 }
		
		 setRedirectUrl(this.getContextPath() + "/cm/renew/edit.do?currentPage="
					+ getCurrentPage() + "&entity.id=" + parent.getId(), "search",
					search);
		return EDIT;
	}
	
	@Action(CREATE)
	public String create() {
		entity.setCreateDate(new Date());// 添加创建时间
		entity.setCmImporting(CopyOfCertificationImporting.RENEW);
		
		parent = certificateRenewService.find(parent.getId());
		entity.setCertificateRenew(parent);
		try {
			renewObjectService.save(entity);
			
			parent.setInSupplyPrice(this.getInSupplePrice(parent));
			certificateRenewService.save(parent);
			setSuccessPopMessage("新增数据成功");
		} catch (Exception e) {
			LOG.error("合格证换证，新增加换入对象出错", e);
			setSuccessPopMessage("合格证换证，新增加换入对象出错!");
		}

		return SHOW_MSG_PAGE;
	}

	@Action(UPDATE)
	public String update() {
		RenewObject renewObject = renewObjectService.find(entity.getId());
		String[] properties = {"financial","shop", "brand", "draft", "motorcycleType", "orderNumber", "chassis", "draftBeginDate", "certificationNum", "draftEndDate", "engine", "supplyPrice"};
		SshBeanUtils.copyProperties(entity, renewObject, properties);
		
		renewObject.setCmImporting(CopyOfCertificationImporting.RENEW);
		parent = renewObject.getCertificateRenew();
		
		try {
			renewObjectService.save(renewObject);
			
			parent.setInSupplyPrice(this.getInSupplePrice(parent));
			certificateRenewService.save(parent);
			
			setSuccessPopMessage("更新数据成功");
		} catch (Exception e) {
			LOG.error("合格证换证，更新换入对象出错", e);
			setSuccessPopMessage("合格证换证，更新换入对象出错!");
		}

		return SHOW_MSG_PAGE;
	}

	private double getInSupplePrice(CertificateRenew renew) {
		double inSupplePrice = 0;
		List<RenewObject> renewObjects = renew.getInCertificateRenews();
		if(renewObjects == null){
			return inSupplePrice;
		}
		for (Iterator<RenewObject> iterator = renewObjects.iterator(); iterator.hasNext();) {
			RenewObject renewObject = iterator.next();
			inSupplePrice += renewObject.getSupplyPrice();
			
		}
		return inSupplePrice;
	}
	
	@Action(DESTORY)
	public String destory() {
		 if(entity == null || entity.getId() == null){
			 setSuccessPopMessage("缺少必要参数ID,请重新尝试");
			 return SHOW_MSG_PAGE;
		 }
		 
		entity = renewObjectService.find(entity.getId());
		parent = entity.getCertificateRenew();
		parent.setInSupplyPrice(parent.getInSupplyPrice()-entity.getSupplyPrice());
		renewObjectService.destory(entity);
		certificateRenewService.save(parent);
		
		setSuccessPopMessage("删除数据成功");
		setRedirectUrl(this.getContextPath() + "/cm/renew/edit.do?currentPage="
				+ getCurrentPage() + "&entity.id=" + parent.getId(), "search",
				search);
		return SHOW_MSG_PAGE;
	}

	public List<Brand> getBrands() {
		return brands;
	}

	public void setBrands(List<Brand> brands) {
		this.brands = brands;
	}

	public List<Financial> getFinancials() {
		return financials;
	}

	public void setFinancials(List<Financial> financials) {
		this.financials = financials;
	}

	public RenewObject getSearch() {
		return search;
	}

	public void setSearch(RenewObject search) {
		this.search = search;
	}

	public RenewObject getEntity() {
		return entity;
	}

	public void setEntity(RenewObject entity) {
		this.entity = entity;
	}

	public FinancialService getFinancialService() {
		return financialService;
	}

	public void setFinancialService(FinancialService financialService) {
		this.financialService = financialService;
	}

	public BrandService getBrandService() {
		return brandService;
	}

	public void setBrandService(BrandService brandService) {
		this.brandService = brandService;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public OrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public CertificateRenew getParent() {
		return parent;
	}

	public void setParent(CertificateRenew parent) {
		this.parent = parent;
	}

	
}
